package com.kfm.loginIp.service.impl;

import com.kfm.loginIp.mapper.UserMapper;
import com.kfm.loginIp.model.User;
import com.kfm.loginIp.service.UserService;
import com.kfm.loginIp.util.MD5Util;
import com.kfm.loginIp.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author Aurora雪
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 登录
     *
     * @param userInput
     * @param password
     * @return
     */
    @Override
    public User login(String userInput, String password) {
        if (userInput == null || password == null || userInput.length() == 0 || password.length() == 0){
            return null;
        }
        if (userInput.matches("^1[3-9]\\d{9}$")) {
            //给密码加密
            String password1 = MD5Util.md5(password);
            return userMapper.findByPhoneAndPassword(userInput, password1);
        } else if (userInput.matches("\\S+@\\S+\\.\\S+")) {
            String password1 = MD5Util.md5(password);
            return userMapper.findByEmailAndPassword(userInput, password1);
        } else {
            String password1 = MD5Util.md5(password);
            return userMapper.findByUsernameAndPassword(userInput, password1);
        }
    }

    /**
     * 检查用户名是否存在
     *
     * @param input
     * @return
     */
    @Override
    public boolean checkUsername(String input) {
        //判断是不是邮箱
        if (input.matches("\\S+@\\S+\\.\\S+")) {
            User user = userMapper.selectByEmail(input);
            if (user != null) {
                return true;
            } else {
                return false;
            }
        } else {
            User user = userMapper.selectByUsername(input);
            if (user != null) {
                return true;
            } else {
                return false;
            }
        }
    }

    /**
     * 检查手机号是否存在
     *
     * @param phone
     * @return
     */
    @Override
    public boolean checkPhone(String phone) {
        if (userMapper.selectByPhone(phone) != null) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 用户名或者邮箱注册
     *
     * @param usernameAndEmail
     * @param namePassword
     * @param rePassword
     * @return
     */
    @Override
    public int register(String usernameAndEmail, String namePassword, String rePassword) {
        if (!namePassword.equals(rePassword)) {
            return 0;
        } else {
            if (namePassword.length() < 6) {
                return 0;
            } else {
                if (usernameAndEmail.matches("\\S+@\\S+\\.\\S+")) {
                    if (userMapper.selectByEmail(usernameAndEmail) != null) {
                        return 0;
                    }
                } else {
                    if (userMapper.selectByUsername(usernameAndEmail) != null) {
                        return 0;
                    }
                }
            }
        }

        User user = new User();
        user.setId(UUIDUtil.uuid32());//生成id
        if (usernameAndEmail.matches("\\S+@\\S+\\.\\S+")) {
            user.setEmail(usernameAndEmail);
        } else {
            user.setUsername(usernameAndEmail);
        }
        //利用MD5加密
        user.setCreateTime(new Date());
        user.setPassword(MD5Util.md5(namePassword));
//        user.setPassword(namePassword);

        // 保存用户信息
        int result = userMapper.insertSelective(user);
        if (result == 1) {
            return 1; // 注册成功
        } else {
            return 0; // 注册失败
        }
    }

    /**
     * 手机注册
     *
     * @param phone
     * @param password1
     * @param rePassword1
     * @return
     */
    @Override
    public int phoneRegister(String phone, String password1, String rePassword1) {
        if (!password1.equals(rePassword1)) {
            return 0;
        }else {
            User user = new User();
            String phone1 = phone.substring(3);
            user.setId(UUIDUtil.uuid32());
            user.setPhone(phone1);
            user.setCreateTime(new Date());
            user.setPassword(MD5Util.md5(password1));
            int result = userMapper.insertSelective(user);
            if (result == 1) {
                return 1; // 注册成功
            } else {
                return 0; // 注册失败
            }
        }
    }

    /**
     * 检查邮箱或者手机号是否存在
     *
     * @param input
     * @return
     */
    @Override
    public boolean checkEmailOrPhone(String input) {
        if (input.matches("^1[3-9]\\d{9}$")) {
            if (userMapper.selectByPhone(input) != null){
                return true;
            }else {
                return false;
            }
        } else if (input.matches("\\S+@\\S+\\.\\S+")){
            if (userMapper.selectByEmail(input)!= null){
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }
    }

    /**
     * 根据手机修改密码
     * @param s
     * @param password
     * @return
     */
    @Override
    public int updatePassword(String s, String password) {
        User user = new User();
        user.setPhone(s);
        user.setPassword(MD5Util.md5(password));
        if (userMapper.updateByPhone(user) == 1){
            return 1;
        }else {
            return 0;
        }
    }

    /**
     * 根据邮箱修改密码
     * @param email
     * @param password1
     * @return
     */
    @Override
    public int updateEmail(String email, String password1) {
        User user = new User();
        user.setEmail(email);
        user.setPassword(MD5Util.md5(password1));
        if (userMapper.updateByEmail(user) == 1){
            return 1;
        }else {
            return 0;
        }
    }


}

